home *** CD-ROM | disk | FTP | other *** search
- Path: doc.ic.ac.uk!not-for-mail
- From: mdf@doc.ic.ac.uk (Martin Frost)
- Newsgroups: comp.sys.amiga.programmer
- Subject: Re: Need help with strings in SAS/C
- Date: 16 Feb 1996 13:48:44 -0000
- Organization: Dept. of Computing, Imperial College, University of London, UK.
- Distribution: world
- Message-ID: <4g21vs$8ar@motmot.doc.ic.ac.uk>
- References: <4fehq5$keg@news.unicomp.net>
- Reply-To: mdf@doc.ic.ac.uk (Martin Frost)
- NNTP-Posting-Host: motmot.doc.ic.ac.uk
- Keywords: SAS C
- X-Newsreader: mxrn 6.18-23
-
-
- In article <4fehq5$keg@news.unicomp.net>, rays@conline.com (Ray Schmalzl) writes:
-
- >void main()
- ^^^^
-
- main() returns an int.
-
- > char CharOne, CharTwo[]="012345";
-
- This should be
-
- char CharOne[2], CharTwo[]="012345";
-
- > CharOne = getch(); // assume typing in a digit here
-
- CharOne[0] = getch();
- CharOne[1] = 0;
-
- The variable definition "char CharOne" simply defines enough space for 1 char.
- The function atoi() takes a *string*, which is a pointer to memory containing
- zero or more chars and then a null char.
-
- The declaration "char CharTwo[]" (equivalent to "char *CharTwo") defines a
- string, which in this case is pre-initialised to point to 7 chars' worth of
- memory containing the hex values 0x30,0x31,0x32,0x33,0x34,0x35,0x00. Because
- of this, atio() will work correctly on CharTwo.
-
- If you want to convert a single digit integer read with getch() then you must
- create a dummy string consisting of the character and then a zero byte. Since
- a string in C is exactly the same as an array of char with the last char zero,
- this is what we do above.
-
- > printf("%i\n",atoi(CharOne)) ;
- ^
- shouldn't that be %d?
-
- > printf("%c\t",CharTwo[3]) ;
-
- this line is correct; CharTwo[3] has type char.
-
- > printf("%i\n",atoi (CharTwo[3]) ;
-
- this line will also cause a typeclash error, as atoi needs a pointer to chars.
-
- >Is there any way to do what I'm trying to do without declaring that extra
- >variable?
-
- The *easiest* way of converting a single digit to its numerical value is to
- do:
-
- int v = (int)getch();
- if(v>='0' && v<='9') v-='0';
- else
- {
- /* char was not a digit */
- }
-
- >And what is the deal with that "ecpecting char const * found
- >char" error?
-
- It means that atoi() needs a pointer (the "char const *") and got a char.
-
- Martin
-